/**
 * Created by xiu on 2017/4/4.
 */
import React from "react";
import { DatePicker} from 'antd';
import Util from "../../../util/Util"
import moment from 'moment';
const dateFormat = 'YYYY-MM-DD';
export default class Creatorder_StepThree extends React.Component{
    constructor(props){
        super(props);
        this.state={
            islast:false,
            isfrist:false,
            data:'',
            context:this.props.context
        }
        this.context_amount = ''
    }

    componentWillMount(){
        let totallenght = this.props.totallenght
        if(totallenght == 1){
            this.setState({
                isfrist:true,
                islast:true,
            });
            return;
        }

        if(((this.props.index+1) == totallenght)){
            this.setState({
                islast:true,
            });
        }

        // if(this.props.index == 0){
        //     this.setState({
        //         isfrist:true,
        //     });
        // }

    }

    addremarkitemlist(){
        this.props.addremarkitemlist();
    }

    deremarkteitem(index){
        this.props.deremarkteitem(index);
    }

    showcalendar(){
        this.props.showcalendar();
    }

    chosedate(date, dateString){
        let context = this.state.context;
        context.deliverDate = dateString;
        this.setState({
            context: context
        });
    }

    _TextChange(contextname,e){
        let text = e.target.value;
        // let rule = /^[0-9,'.']*$/;
        let regular = /^[0-9]+\.?[0-9]{0,3}$/;
        let regular1 = /^[0-9]{0,10}$/
        let context = this.state.context;
       // this.props.order_type
        if(contextname=='amount'){
            if(this.props.order_type == 'createordergms' || this.props.order_type == 'entryordergms') {
                if(!regular1.test(text) && text != ''){
                    // context[contextname] = this.context_amount;
                    e.target.value = ''
                    // console.log(text);
                    return;
                }else{
                    this.context_amount = text;
                    context[contextname] = this.context_amount;
                    this.setState({
                        context:context
                    });
                }
            } else {
                if(!regular.test(text) && text != ''){
                    // context[contextname] = this.context_amount;
                    e.target.value = ''
                    // console.log(text);
                    return;
                }else{
                    this.context_amount = text;
                    context[contextname] = this.context_amount;
                    this.setState({
                        context:context
                    });
                }
            }

        }else{
            context[contextname] = text;
            this.setState({
                context:context
            });
        }
    }

    disabledDate(current){
        return current && current.valueOf() < Date.now();
    }

    render(){
        // console.log(Util.dateFormat(this.state.context.deliverDate));
        return (
            <thead>
                <tr className="order_from_trcontext">
                    <td className="order_frorm_td order_frorm_td_three_time">
                        <div className="order_frorm_td_div">
                            <DatePicker className="ant-calendar-picker-input" onChange={this.chosedate.bind(this)} ref={'dpicker'} format={dateFormat}
                                        placeholder="请选择日期"
                                        defaultValue={this.state.context.deliverDate?moment(Util.dateFormat(this.state.context.deliverDate), dateFormat):''}
                                        disabledDate={this.disabledDate.bind(this)}/>
                        </div>
                        </td>
                    <td className="order_frorm_td order_frorm_td_three_num"><input className="order_from_inputcontext" value={this.state.context.amount} onChange={this._TextChange.bind(this,'amount')}/></td>
                    <td className="order_frorm_td order_frorm_td_three_remark"><input className="order_from_inputcontext" value={this.state.context.remark} onChange={this._TextChange.bind(this,'remark')}/></td>
                    <td className="order_frorm_td order_from_td_delandadd" >
                        <span className={this.state.islast?'order_from_sp_add':'order_from_sp_addmiss'} onClick={this.addremarkitemlist.bind(this)} />
                        <span className={this.state.isfrist?"order_from_sp_minusmiss":"order_from_sp_minus"} onClick={this.deremarkteitem.bind(this,this.props.index)} />
                    </td>
                </tr>
            </thead>
        )
    }
}
